﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.stage;
using Imaginecup.util;
using Imaginecup.util.script.malaria;
using Imaginecup.gameState.malaria.manager;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.component;
using Imaginecup.gameState.malaria.componentInit;
using Imaginecup.util.particle;
using Imaginecup.loop;
using Imaginecup.gameState.malaria.loop;
using Imaginecup.gameState.malaria.component.scene;
using Microsoft.Xna.Framework;
using Imaginecup.observer;
using Imaginecup.gameState.malaria.component.factory;
using Imaginecup.factory;
using Imaginecup.gameState.malaria.component.entity.panel;
using Imaginecup.gameState.malaria.component.panel.clickAble;
using imaginecup.util;
using Imaginecup.componentSystem.component;
using Imaginecup.component.scene;

namespace Imaginecup.gameState.malaria.stage
{
    public class Stage4 : MalariaBaseStage, IStageLoop, IStage 
    {
            private ObjectManager m_ObjectMan;

        public Stage4()
        {
            int life = 3;

//#if WINDOWS
//            ScriptReader reader = ScriptReader.GetScriptReader();
//            reader.LoadDocument("Stage1.xml");
 
//            life = reader.GetDataToInt("목숨수");
//#endif

            DrawNumber drawNumber = new DrawNumber("Time");
            LifeMan.GetLifeMan().SetLife(life);

       
            SceneMan sceneMan = new SceneMan( this );
            sceneMan.SetDrawNumber(drawNumber);

            m_SceneMan = sceneMan;
                        
            //오브젝트 매니저에서 사용할 데이터 초기화.
            ObjectManagerData objManData = new ObjectManagerData(new MalariaCompInit());                        
            SetFieldInfo(objManData);            
                      


            //디폴트로 제일 처음 미니맵 화면을 선택한다.
            ICmpScene minimap = m_SceneMan.GetScene(0);
            minimap.SetSelectedScene();
            
            GameResourceMan.GetResourceMan().SetMoney(100);
        }

        protected override void UnloadResource()
        {
            base.UnloadResource();
        }

#if WINDOWS
        protected override void CheatKey()
        {
            ChangeStage(StageEnum.Stage5);
        }
#endif
        public override void CheckGameState()
        {
            if (LifeMan.GetLifeMan().GetLifeCount() == 0)
            {
                SetGameOver();
            }


            else if (m_ObjectMan.GetNumofComponent(ComponentTypeID.CID_CHILDREN_ENTITY) == 0 && m_ChildGenerator.GetChildNum() == 0)
            {
                ChangeStage(StageEnum.Stage5);
            }

        }
                

        #region 실제 게임 필드에 사용될 정보들의 초기화(세팅) 부분
        private void SetFieldInfo(ObjectManagerData objManData)
        {
            TouchEffectGenerator.SetEnableData(Vector2.Zero, MalariaState.GetMalariaState().GetMainScreenSize());

            m_ObjectMan = new ObjectManager();
            m_ObjectMan.Init(objManData);

            ICmpScene cmpScene = CreateScene(m_ObjectMan);                      
            
            ICmpEntity hero = (ICmpEntity)ObjectFactory.GetObjectFactory().GetObject(MALARIA_COMPONENT_ID.HERO);
            hero.SetEntityInfo(new EntityInfo(50, 150, 50, 50));

            ICmpEntity home = (ICmpEntity)ObjectFactory.GetObjectFactory().GetObject(MALARIA_COMPONENT_ID.HOME);
            home.SetEntityInfo(new EntityInfo(10, 210, 150, 150));

            m_ChildGenerator = new ChildGenerator();
            m_ChildGenerator.SetObjectManager(m_ObjectMan);

            m_MalariaGenerator = new MalariaGenerator();
            m_MalariaGenerator.SetObjectManager(m_ObjectMan);

            SetMenuIcon(m_ObjectMan);
//#if WINDOWS

//            ScriptReader script = ScriptReader.GetScriptReader();
//          //  script.ReadObjectData(m_ObjectMan);


//            script.ReadCharInfo(m_ChildGenerator);
//            script.ReadMalaria(m_MalariaGenerator);

//            SetClickAblePanel(m_ObjectMan);

//#endif

//#if WINDOWS_PHONE|| XBOX

            CreateChild();
            CreateMalaria();
            SetClickAblePanel(m_ObjectMan);
//#endif
            IUpdateAble updateAble = new UpdateObjectMan(m_ObjectMan);
            IDrawAble drawAble = new DrawObjectMan(m_ObjectMan);

            cmpScene.SetSceneData(new SceneData(m_ObjectMan, drawAble, updateAble));
            m_SceneMan.AddScene(cmpScene);
        }

        private void CreateChild()
        {
            m_ChildGenerator.AddChildInfo(new ChildInfo(1, 20));
        }
       
        private void CreateMalaria()
        {
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 20));
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 20));
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 60));
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 25));
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 30));
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 50));
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 40));
            m_MalariaGenerator.AddChildInfo(new ChildInfo(1, 30));
        }
        private void SetClickAblePanel(ObjectManager objMan)
        {
            CommunicationMan clickAbleCommunication = new CommunicationMan();

            EntityInfo panelInfo = new EntityInfo(21, 370, 84, 106);
            int panelGap = 88;

            ObjectFactory factory = ObjectFactory.GetObjectFactory();
            factory.SetObjectMan(objMan);

            CmpClickAblePanel entity = (CmpClickAblePanel)factory.GetObject(MALARIA_COMPONENT_ID.ATTACK_MOVE_PANEL);
            entity.SetEntityInfo(panelInfo);
            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);

            panelInfo.mPosition.X += panelGap;

            entity = (CmpClickAblePanel)factory.GetObject(MALARIA_COMPONENT_ID.ATTACK_HAND_PANEL);
            entity.SetEntityInfo(panelInfo);
            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);


            panelInfo.mPosition.X += panelGap;

            entity = (CmpClickAblePanel)factory.GetObject(MALARIA_COMPONENT_ID.ATTACK_SLICE_PANEL);
            entity.SetEntityInfo(panelInfo);
            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);



            panelInfo.mPosition.X += panelGap;

            entity = (CmpClickAblePanel)factory.GetObject(MALARIA_COMPONENT_ID.ITEM_ID_SHIELD_PANEL);
            entity.SetEntityInfo(panelInfo);
            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);


            panelInfo.mPosition.X += panelGap;

            entity = (CmpClickAblePanel)factory.GetObject(MALARIA_COMPONENT_ID.ITEM_ID_PILL_PANEL);
            entity.SetEntityInfo(panelInfo);
            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);


            panelInfo.mPosition.X += panelGap;

            entity = (CmpClickAblePanel)factory.GetObject(MALARIA_COMPONENT_ID.ITEM_ID_WEB_PANEL);
            entity.SetEntityInfo(panelInfo);
            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);


            panelInfo.mPosition.X += panelGap;

            entity = (CmpClickAblePanel)factory.GetObject(MALARIA_COMPONENT_ID.ITEM_ID_FROG_PANEL);
            entity.SetEntityInfo(panelInfo);
            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);

            clickAbleCommunication.RegisterCommunicationWithRelease();
        }


        private ICmpScene CreateScene(ObjectManager objMan)
        {
            ObjectFactory factory = ObjectFactory.GetObjectFactory();
            factory.SetObjectMan(objMan);
           
       //     objTypeID.TextureName = "UI";
       //     objTypeID.RenderID = RENDER_TYPE_ID.

            ICmpScene factoryUI = (ICmpScene)factory.GetObject(MALARIA_COMPONENT_ID.FIELD_SCENE);
            return factoryUI;
        }
                
       
        #endregion
      

    }
}
